package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.minio.MinioConfiguration;
import com.atguigu.lease.common.minio.MinioProperties;
import com.atguigu.lease.web.admin.service.FileService;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.SneakyThrows;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {
    @Autowired
    MinioConfiguration minioConfiguration;
    @Autowired
    MinioClient minioClient;
@Autowired
    MinioProperties minioProperties;
    @SneakyThrows
    @Override
    public String upload(MultipartFile file) {
//        String fileName = file.getOriginalFilename();   // 获取文件名
        String contentType = file.getContentType();    // 获取文件类型
        String bucketName = UUID.randomUUID().toString(); // 生成随机字符串作为桶名
        String path = new DateTime().toString("YYYY/MM/DD/"); // 生成文件路径
        String filePath=path+bucketName+"."+contentType;

PutObjectArgs putObjectArgs = PutObjectArgs.builder()
        .bucket(minioProperties.getBucketName())
        .object(filePath)
        .stream(file.getInputStream(), file.getSize(), -1)
        .build();

        return minioProperties.getEndpoint() + "/" + minioProperties.getBucketName()+filePath;
    }
}
